-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[@xstate/store] Skip events from undo/redo #5393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: be2f559 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
@davidkpiano maybe |
I've thought of this too but what use-case are you thinking? |
Depends on the undo/redo API and how transaction ids are passed. Do you have an example of that? |
const store = createStore(
undoRedo(
{
context: { count: 0 },
on: {
inc: (ctx, ev: { t?: string }) => ({ count: ctx.count + 1 }),
dec: (ctx) => ({ count: ctx.count - 1 })
}
},
{
getTransactionId: (event) => {
return event.t;
}
}
)
);
store.trigger.inc(); // not part of a transaction
store.trigger.inc({ t: 'a' }); // part of 'a' transaction
store.trigger.inc({ t: 'a' }); // part of 'a' transaction
store.trigger.inc({ t: 'a' }); // part of 'a' transaction
store.trigger.inc({ t: 'b' }); // part of 'b' transaction
store.trigger.inc({ t: 'b' }); // part of 'b' transaction
store.trigger.inc({ t: 'b' }); // part of 'b' transaction
store.trigger.undo(); // undoes three events from 'b' transaction
store.trigger.undo(); // undoes three events from 'a' transaction
store.trigger.undo(); // undoes a single event |
getTransactionId?: ( | ||
event: ExtractEvents<TEventPayloadMap>, | ||
snapshot: StoreSnapshot<TContext> | ||
) => string | null | undefined; | ||
/** | ||
* A function that returns whether an event should be skipped during | ||
* undo/redo. Skipped events are not stored in history and are not replayed | ||
* during undo/redo. | ||
*/ | ||
skipEvent?: ( | ||
event: ExtractEvents<TEventPayloadMap>, | ||
snapshot: StoreSnapshot<TContext> | ||
) => boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: event actions API has order ctx, event
whereas these two functions have the reverse order. They primarily work based on events which makes sense for event to be the first parameter but for consistency, we might want to consider unifying the order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That'd be a breaking change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall LGTM
Add
skipEvent
option toundoRedo()
to exclude certain events from undo/redo history.Add
snapshot
parameter togetTransactionId
function.